package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.response.GameStarterIndexResponse;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.ItemGridDownloadView;
import cn.wit.summit.chwan.widgets.ItemListPartView;

/**
 * Created by Alan on 2018/4/4.
 */

public class GameStarterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;

    private Map<String, String> downTemp = new HashMap<>();

    public GameStarterAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    public void clearTempData () {
        downTemp.clear();
    }

    public boolean setItemDataByPo(int po, GameStarterIndexResponse temp) {
        boolean isFull = false;
        ViewBean viewBean = mDatas.get(po);
        if (viewBean.viewType == GameStarterAdapter.ViewType.TYPE_GRID) {
            List<GameStarterIndexResponse> list = (List<GameStarterIndexResponse>) viewBean.getObj();
            list.add(temp);
            if (list.size()==3) {
                isFull = true;
            }
        }
        return isFull;
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size()>0 && downTemp.containsKey(gameId)){
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (mDatas.get(itemPo).getViewType() == GameStarterAdapter.ViewType.TYPE_GRID) {
            List<GameListBaseBean> list = (List<GameListBaseBean>) mDatas.get(itemPo).getObj();
            return list.get(po);
        } else if (mDatas.get(itemPo).getViewType() == GameStarterAdapter.ViewType.TYPE_LIST) {
            GameListBaseBean list = (GameListBaseBean) mDatas.get(itemPo).getObj();
            return list;
        }
        return null;
    }

    public enum ViewType {
        TYPE_LINE,
        TYPE_LINE_SMALL,
        TYPE_TITLE,
        TYPE_IMAGE,
        TYPE_LIST,
        TYPE_GRID
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public static class TitleBean {
            public String time;

            public TitleBean(String time) {
                this.time = time;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_TITLE.ordinal()) {
            holder = new TitleHolder(inflater.inflate(R.layout.item_daily_new_title_view, parent, false));
        } else if (viewType == ViewType.TYPE_GRID.ordinal()) {
            holder = new GridHolder(inflater.inflate(R.layout.item_game_starter_grid_view, parent, false));
        } else if (viewType == ViewType.TYPE_LINE.ordinal()) {
            holder = new LineHolder(inflater.inflate(R.layout.item_game_starter_line_view, parent, false));
        } else if (viewType == ViewType.TYPE_LINE_SMALL.ordinal()) {
            holder = new LineSmallHolder(inflater.inflate(R.layout.item_daily_new_small_no_padding_line_view, parent, false));
        } else if (viewType == ViewType.TYPE_LIST.ordinal()) {
            holder = new ListHolder(inflater.inflate(R.layout.item_game_starter_list_view, parent, false));
        } else if (viewType == ViewType.TYPE_IMAGE.ordinal()) {
            holder = new ImageHolder(inflater.inflate(R.layout.item_game_starter_image_view, parent, false));
        }

        return holder;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof LineHolder) {
        } else if (holder instanceof LineSmallHolder) {
        } else if (holder instanceof TitleHolder) {
            ViewBean.TitleBean temp = (ViewBean.TitleBean) viewBean.getObj();
            TitleHolder titleHolder = (TitleHolder) holder;
            titleHolder.titleRightTv.setText(temp.time);
        } else if (holder instanceof ImageHolder) {
            final GameStarterIndexResponse temp = (GameStarterIndexResponse) viewBean.getObj();
            ImageHolder imageHolder = (ImageHolder) holder;
            if (temp!=null && !StringUtils.isEmpty(temp.getGame_the_first_pic()))
                imageHolder.itemGameStarterIv.setImageURI(temp.getGame_the_first_pic());
            else {
                Uri uri = Uri.parse("res:///" + R.drawable.main_normal_horizontal_short_icon);
                imageHolder.itemGameStarterIv.setImageURI(uri);
            }
            imageHolder.itemGameStarterIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtil.getInstance().goGamedetial(mContext,temp.getGame_id());

                }
            });
        }  else if (holder instanceof ListHolder) {
            GameStarterIndexResponse temp = (GameStarterIndexResponse) viewBean.getObj();
            ListHolder listHolder = (ListHolder) holder;
            if (temp!=null) {
                downTemp.put(temp.getGame_id(), position+"");
                listHolder.itemListPartView.setData(mContext, temp, false);
            }
        } else if (holder instanceof GridHolder) {
            List<GameStarterIndexResponse> list = (List<GameStarterIndexResponse>) viewBean.getObj();
            GridHolder gridHolder = (GridHolder) holder;

            gridHolder.itemGridDownloadViewOne.setVisibility(View.INVISIBLE);
            gridHolder.itemGridDownloadViewTwo.setVisibility(View.INVISIBLE);
            gridHolder.itemGridDownloadViewThree.setVisibility(View.INVISIBLE);

            for (int i=0;i<list.size();i++) {
                GameStarterIndexResponse temp = list.get(i);
                downTemp.put(temp.getGame_id(), position+"|"+i);
                switch (i) {
                    case 0:
                        gridHolder.itemGridDownloadViewOne.setVisibility(View.VISIBLE);
                        gridHolder.itemGridDownloadViewOne.setData(temp, true);
                        break;
                    case 1:
                        gridHolder.itemGridDownloadViewTwo.setVisibility(View.VISIBLE);
                        gridHolder.itemGridDownloadViewTwo.setData(temp, true);
                        break;
                    case 2:
                        gridHolder.itemGridDownloadViewThree.setVisibility(View.VISIBLE);
                        gridHolder.itemGridDownloadViewThree.setData(temp, true);
                        break;
                }
            }
        }
    }

    class LineHolder extends RecyclerView.ViewHolder {

        public LineHolder(View view) {
            super(view);
        }
    }

    class LineSmallHolder extends RecyclerView.ViewHolder {

        public LineSmallHolder(View view) {
            super(view);
        }
    }

    class ListHolder extends RecyclerView.ViewHolder {
        ItemListPartView itemListPartView;

        public ListHolder(View view) {
            super(view);
            itemListPartView = view.findViewById(R.id.itemListPartView);
        }
    }

    class ImageHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView itemGameStarterIv;

        public ImageHolder(View view) {
            super(view);
            itemGameStarterIv = view.findViewById(R.id.itemGameStarterIv);
        }
    }

    class TitleHolder extends RecyclerView.ViewHolder {
        TextView titleRightTv;

        public TitleHolder(View view) {
            super(view);
            titleRightTv = view.findViewById(R.id.titleRightTv);
        }
    }

    class GridHolder extends RecyclerView.ViewHolder {

        ItemGridDownloadView itemGridDownloadViewOne;
        ItemGridDownloadView itemGridDownloadViewTwo;
        ItemGridDownloadView itemGridDownloadViewThree;

        public GridHolder(View view) {
            super(view);
            itemGridDownloadViewOne = view.findViewById(R.id.itemGridDownloadViewOne);
            itemGridDownloadViewTwo = view.findViewById(R.id.itemGridDownloadViewTwo);
            itemGridDownloadViewThree = view.findViewById(R.id.itemGridDownloadViewThree);
        }
    }
}
